package es.albarregas.servlet;

import java.io.IOException;
import java.util.Collection;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import es.albarregas.modelo.*;

/**
 * Servlet implementation class UserAvailableServlet
 * 
 * Comprueba si existe un username introducido en el formulario de registro.
 * 
 */
@WebServlet("/UserAvailableServlet.jsp")
public class UserAvailableServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		boolean libre;
		String cwhere = new String("");
		cwhere = "where username=\'"+request.getParameter("inputUsername")+"\'";
		//System.out.println(cwhere);
		Collection<User> listaUsuarios = ModelFacade.getUser(cwhere);
		libre = listaUsuarios.isEmpty();
		//System.out.println("******libre "+libre);
		response.setStatus(HttpServletResponse.SC_OK);  
        response.setContentType("application/json");  
        response.getWriter().print(libre);  
	}
	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		doGet(request, response);
		
		
		
	}


}
